package Response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*
 3: 设置响应体:
         使用步骤:
            1: 获取输出流
                 1: 字符输出流 : printWriter getWriter()
                 2: 字节输出流 : ServletOutputStream getOutputStream()
            2: 使用输出流,将数据输出到客户端浏览器
                  乱码问题 :
                   简单的形式,设置编码 : resp.setContentType("text/html;charset=utf-8");

*/
@WebServlet("/responseDemo04")
public class Response04 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //每次获取流对象之前,设置流的默认编码: ISO-8859-1设置为GBK
            //resp.setCharacterEncoding("GBK");
        //告诉浏览器,服务器发送的消息数据的编码.让浏览器使用该编码解码
        //resp.setHeader("content-type","text/html;charset=GBK");//这一行代码包含了上一行代码
             //简单的形式设置编码
          resp.setContentType("text/html;charset=utf-8");
        // 获取字符输出流
        PrintWriter aa = resp.getWriter();
        //输出数据
        //aa.write("<h1>hello response</h1>");
        aa.write("你好 response");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
